<?php
/**
 *============================================================================
 * Cornice PHP Framework & Website Builder
 * Freeware (f) 2011 by Inforo Software & Computer
 *============================================================================
 */
 
/**
 * Class processing HTTP request
 * @author Nicola Dinh
 * @version 2011.04.2217
 */
class request {

  /*
   * All properties
   */
  private static $module = null;
  private static $page = null;
  private static $validated_url = null;
  private static $url_params = array();
  private static $form_params = array();

  /**
   * Default instance level constructor
   * Just decorative, should not be called due to having only static methods
   */
  public function __construct() {
    //
  }
  
  /**
   * Parse module name specified in URL (eg. http://domain.ext/module_page_...)
   */
  private static function parse_module_name() {
    $query_string = trim($_SERVER["QUERY_STRING"]); 
    if (strlen($query_string)==0) {
      self::$module = config::get_default_module(); 
      return;
    }
    
    //no page name
    if (strpos($query_string,"_")===false) {
      self::$module = $query_string;
      return;
    }
    
    //with page name
    $tokens = explode("_",$query_string);
    self::$module = $tokens[0]; 
  }
  
  /**
   * Get module name in HTTP request
   * @return string The module name
   */
  public static function get_module_name() {
    return self::$module;
  }
  
  /**
   * Parse page name specified in URL (eg. http://domain.ext/module_page_...)
   */
  private static function parse_page_name() {
    $module = self::get_module_name(); 
    $module_xml = "modules/$module.xml";
    if (!file_exists($module_xml)) {
      response::error("'$module_xml' not found");
      exit;
    }
    
    //load xml
    $module_info = xml::arr_from_file($module_xml); 
    
    //no query string
    $query_string = trim($_SERVER["QUERY_STRING"]);    
    if (strlen($query_string)==0) {
      self::$page = utils::get_arr_value($module_info,"page/default");    
      return;
    }
      
    //no page name
    if (strpos($query_string,"_")===false) {
      self::$page = utils::get_arr_value($module_info,"page/default");
      return;
    }
      
    //with page name
    $tokens = explode("_",$query_string); 
    self::$page = $tokens[1];
  }  
 
  /**
   * Get page name specified in URL
   * @return string The page name
   */
  public static function get_page_name() {
    return self::$page;
  }
 
  /**
   * Make validated URL based on request URL
   * @return string The validated url (meaning correct format)
   */
  private static function make_validated_url() {
    $module = self::get_module_name();
    $page = self::get_page_name();
    
    //no query string
    $query_string = trim($_SERVER["QUERY_STRING"]);
    if (strlen($query_string)==0) {
      self::$validated_url = $module."_".$page;
      return;
    }
      
    //no page name
    if (strpos($query_string,"_")===false) {
      self::$validated_url = $module."_".$page;
      return;
    }
      
    //with page name
    $tokens = explode("_",$query_string);
    $count = floor(count($tokens)/2);
    
    //make url
    $url = $module."_".$page;
    for ($index=1; $index<$count; $index++)
      $url .= "_".$tokens[$index*2]."_".$tokens[$index*2+1];
    
    self::$validated_url = $url;
  }
  
  /**
   * Get validated URL after reformatting
   * @return string The formatted relative URL
   */
  public static function get_validated_url() {
    return self::$validated_url;
  }
  
  /**
   * Obtain parameters in URL
   */
  private static function obtain_url_params() {
    $tokens = explode("_",self::$validated_url);
    for ($index=1; $index<count($tokens)/2; $index++)
      self::$url_params[trim($tokens[$index*2])] = $tokens[$index*2+1];
  }
  
  /**
   * Get URL parameter
   * @param string $name The name of parameter in URL
   * @return string The value of param or 'null' if no such param
   */
  public static function get_url_var($name) {
    return isset(self::$url_params[$name])? self::$url_params[$name]:null;
  }
  
  /**
   * Get form parameter
   * @param string $name The name of parameter in form (by POST)
   * @return string The value of param or 'null' if no such param
   */
  public static function get_form_var($name) {
    return isset($_POST[$name])? $_POST[$name]:null;
  }
  
  /**
   * Get HTTP request method
   * @return string The name of method, either 'post', or 'get',...
   */
  public static function http_method() {
    return strtolower($_SERVER["REQUEST_METHOD"]);
  }
  
  /**
   * Check either is HTTP POST method
   * @return boolean 'true' if is HTTP POST, 'false' otherwise
   */
  public static function is_post() {
    return self::http_method()=="post";
  }

  /**
   * Check either is HTTP GET method
   * @return boolean 'true' if is HTTP GET, 'false' otherwise
   */
  public static function is_get() {
    return self::http_method()=="get";
  }
  
  /**
   * Get query string
   * @return string The value of query string in URL
   */
  public static function get_query_string() {
    return $_SERVER["QUERY_STRING"];
  }
  
  /**
   * Initialise & parse HTTP request
   */
  public static function initialise() {  
    self::parse_module_name(); 
    self::parse_page_name(); 
    self::make_validated_url();
    self::obtain_url_params();
  }
}